﻿using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Newtonsoft.Json;
using System.IO;

namespace oled
{
    public class TcpServer
    {
    }

    partial class Simulator
    {
        Socket socketSend;
        /// <summary>
        /// 监听按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TCP_Server_init()
        {
            try   //开启网络连结的地方，建议都加上try
            {
                //创建负责监听的socket
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, 
                                                SocketType.Stream, 
                                                ProtocolType.Tcp);
                //创建IP地址
                IPAddress ip = IPAddress.Any;

                //创建端口号
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(Port.Text));

                //Socket绑定IP地址与端口号
                socketWatch.Bind(point);

                //设置监听队列
                socketWatch.Listen(10);

                Thread listen = new Thread(Listen);
                listen.IsBackground = true;
                listen.Start(socketWatch);
            }
            catch
            { }
        }

        /// <summary>
        /// 等待客户端的连结，并且创建一个与之对应的Socket
        /// </summary>
        void Listen(object o)
        {
            Socket socketWatch = o as Socket;//将object转化为Socket
            //创建一个负责通信的Socket，用于接收客户端的连结
            while (true)
            {
                try
                {
                    socketSend = socketWatch.Accept();
                    //ShowMsg  "连结成功"
                    //开启一个新的线程，不停的接收客户端发来的消息
                    Thread receive = new Thread(Receive);
                    receive.IsBackground = true;
                    receive.Start(socketSend);
                }
                catch
                { }
            }
        }

        /// <summary>
        /// 服务器不停的接收客户端发来的消息
        /// </summary>
        /// <param name="ob"></param>
        void Receive(object ob)
        {
            Socket socketSend = ob as Socket;
            while (true)
            {
                try
                {
                    byte[] EchoText = Encoding.UTF8.GetBytes("Echo");
                    //客户端连结成功后，服务器应该接受客户端发来的消息
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    //实际接收到的有效字节数
                    int r = socketSend.Receive(buffer);
                    //当不接受数据的时候，则跳出
                    if (r == 0) break;

                    //从0开始解码到r个
                    string tcp_str = Encoding.UTF8.GetString(buffer, 0, r);
                    Json_unpck instance = new Json_unpck();
                    if (instance.unpackTo_oled(tcp_str) > 0)
                    {
                        while (oled_Refresh) ;
                        oled_Refresh = true;
                        socketSend.Send(EchoText);
                    }
                }
                catch
                { }
            }
        }
        void ShowMsg(string str)
        {
        }

    }
}
